/* v0.7c */
function VideoMo(videoId,videosource) {
	this.videoId = videoId;
	this.videosource = videosource || [];
	//if(( /iPhone|iPad|iPod|Android|webOS|BlackBerry/i.test(navigator.userAgent) )) {
	if(( /iPhone|iPod/i.test(navigator.userAgent) )) { // JWPlayer with BTN Resolution
		//jwplayer(videoId+"_vdo").setup({width:"100%", aspectratio: '4:3' , file: videosource[0].file , type:"video/mp4" , provider: "video"});
		jwplayer(videoId+"_vdo").setup({width:"100%" , aspectratio: "4:3" , 
		playlist: [{
			sources: videosource
		}]});

		if(videosource.length > 1) {
			jQuery('#'+videoId ).append('<div class="vdoResolution"></div>');
			for(var i = 0;i < videosource.length;i++){
				jQuery('#'+videoId +' .vdoResolution').append('<div class="btnResolution'+(i == 0?' selected':'')+'" data-src="'+videosource[i].file+'">'+videosource[i].label+'</div>');
			}
		}
		jQuery('.btnResolution').off("click").on("click", function(){
			var videoCId = jQuery(this).parent().parent().attr('id');
			jQuery(this).parent().find('.selected').removeClass('selected');
			jQuery(this).addClass('selected');

			jwplayer(videoCId+"_vdo").setCurrentQuality(jQuery(this).index());
			/*
			jwplayer(videoId+"_vdo").remove();
			jwplayer(videoId+"_vdo").setup({width:"100%", aspectratio: '4:3' ,file: jQuery(this).data('src'),label: jQuery(this).html(),type:"video/mp4",provider: "video"});
			*/
		});

		jwplayer(videoId+"_vdo").onPause(function(e) {
			jQuery('#'+videoId+' .vdoResolution').show();
			jQuery('#'+videoId+' .jwdisplayIcon').show().animate({opacity:1},0);
			//jQuery('#'+videoId+'_vdo .jwvideo').css('position','absolute').css('top','-9999px');
			jQuery('#'+videoId+'_vdo .jwvideo').css('visibility','hidden');
			//jQuery('#'+videoId+' video').hide();
		});

		jwplayer(videoId+"_vdo").onBuffer(function(e) {
			jQuery('#'+videoId+' .vdoResolution').hide();
			//jQuery('#'+videoId+' video').css('position','relative').css('left','0px');
			jQuery('#'+videoId+'_vdo .jwvideo').css('visibility','visible');
			//jQuery('#'+videoId+' video').show();
		});
		jwplayer(videoId+"_vdo").onPlay(function(e) {
			jQuery('#'+videoId+' .vdoResolution').hide();
			//jQuery('#'+videoId+'_vdo .jwvideo').css('position','relative').css('top','0px');
			jQuery('#'+videoId+'_vdo .jwvideo').css('visibility','visible');
			//jQuery('#'+videoId+' video').show();
		});
	} else if(( /Android|webOS|BlackBerry|PlayBook|bb10/i.test(navigator.userAgent) )) { // HTML5 with BTN Resolution
		videoPlayerId = createVideo(videoId,videosource[0].file);
		if(videosource.length > 1) {
			jQuery('#'+videoId ).append('<div class="vdoResolution"></div>');
			for(var i = 0;i < videosource.length;i++){
				jQuery('#'+videoId +' .vdoResolution').append('<div class="btnResolution'+(i == 0?' selected':'')+'" data-src="'+videosource[i].file+'">'+videosource[i].label+'</div>');
			}
		}
		jQuery('#'+videoId+' .btnResolution').on("click", function(){
			var videoCId = jQuery(this).parent().parent().attr('id');
			jQuery(this).parent().find('.selected').removeClass('selected');
			jQuery(this).addClass('selected');

			videoPlayerId = createVideo(videoId,videosource[jQuery(this).index()].file);
		});

		jQuery('#'+videoId ).append('<div class="smcontrols"><div class="smdisplayIcon"><div class="smIconPlay"></div></div></div>');
		jQuery('#'+videoId+' .smdisplayIcon').off("click").on("click", function(){
			var videoCId = jQuery(this).parent().parent().attr('id');
			jQuery('#'+videoCId).find('video')[0].play();
		});
	} else { // JWPlayer only
		jwplayer(videoId+"_vdo").setup({width:"100%" , aspectratio: "4:3" , 
		playlist: [{
			sources: videosource
		}]});
	}
}
var createVideo = function(videoId,srcVideo) {
	var videoPlayerId = videoId+'_vdo_player';
	this.videoPlayerId = videoPlayerId;
	this.videoId = videoId;
	jQuery('#'+videoId+'_vdo').addClass('siamPlayer').addClass('aspectMode').html('<div class="smvdo"><video id="'+videoPlayerId+'" src="'+srcVideo+'" controls></video></div>');

	jQuery('#'+videoPlayerId).on('play',function(){
		// debugConsoleLog('--- play '+videoId+'---');
		jQuery('#'+videoId+' .vdoResolution , #'+videoId+' .smdisplayIcon').hide();
		jQuery('#'+videoId+'_vdo .smvdo').css('visibility','visible');
	});
	jQuery('#'+videoPlayerId).on('pause',function(){
		// debugConsoleLog('--- pause '+videoId+'---');
		jQuery('#'+videoId+' .vdoResolution , #'+videoId+' .smdisplayIcon').show();
		jQuery('#'+videoId+'_vdo .smvdo').css('visibility','hidden');
	});
	jQuery('#'+videoPlayerId).on('loaddata',function(){
		// debugConsoleLog('--- loaddata '+videoId+'---');
	});

	return videoPlayerId;
}
/*var debugConsoleLog = function(eMsg){
	console.log(eMsg);
	jQuery('#debugConsole').append(eMsg+"<br />\r\n");
}
jQuery(document).ready(function(){
	jQuery('#debugConsole').append("version: 0.6b<br />\r\n");
})*/